//
//  UpdateViewController.m
//  DaysComicSugar
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "UpdateViewController.h"
#import "UpdateCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UpdateModel.h"
#import "UIImageView+WebCache.h"
#import "UpdataDetailViewController.h"

@interface UpdateViewController ()
@property(nonatomic,retain)NSMutableArray *modelArr;
@property(nonatomic,retain)UICollectionView *myCollectionView;
@property(nonatomic, retain)UpdateModel *update;
@end

@implementation UpdateViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self.title=@"每日推荐";
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr=[[NSMutableArray alloc]init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self DataJson];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]];
    //创建一个瀑布流
    UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize=CGSizeMake(100  , 150);
    //最小列间距
    flowlayout.minimumInteritemSpacing=15;
    //最小行间距
    flowlayout.minimumLineSpacing=1;
    flowlayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    flowlayout.sectionInset=UIEdgeInsetsMake(20, 20, 20, 20);
    
    self.myCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)  collectionViewLayout:flowlayout] ;
    self.myCollectionView.backgroundColor=[UIColor whiteColor];
    
    self.myCollectionView.dataSource=self;
    self.myCollectionView.delegate=self;
    [self.view addSubview:self.myCollectionView];
    //注册cell
    [self.myCollectionView registerClass:[UpdateCell class] forCellWithReuseIdentifier:@"reuse"];
    
}
// 网络数据解析
- (void)DataJson
{

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=dailyupdate&pageindex=0&pagesize=41" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSMutableArray *arr=responseObject;
        for (int i = 0; i < arr.count; i++) {
            self.update=[[UpdateModel alloc]init];
            [self.update setValuesForKeysWithDictionary:arr[i]];
            [self.modelArr addObject:self.update];
        }
        NSLog(@"%ld", self.modelArr.count);
//        for (NSMutableDictionary *dic in arr)
//        {
//            UpdateModel *model=[[UpdateModel alloc]init];
//            [model setValuesForKeysWithDictionary:dic];
//            NSLog(@"%@",model);
//            [self.modelArr addObject:model];
//            [model release];
//            NSLog(@"%ld",self.modelArr.count);
//        }
        [self.myCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析错误");
        NSLog(@"%@",error);
        
    }];
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.modelArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UpdateCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    self.update=self.modelArr[indexPath.row];
    cell.mylabel.text=self.update.bookName;
    cell.numberLabel.text=self.update.BookUpdateSection;
//    cell.numberLabel.textColor = [UIColor redColor];
    NSLog(@"%@",self.update.BookUpdateSection);
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.update.bookIconOtherURL]];
    
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
    UpdataDetailViewController *upVC = [[UpdataDetailViewController alloc] init];
    upVC.updateDetail=self.modelArr[indexPath.row];
    [self.navigationController pushViewController:upVC animated:YES];
    [upVC release];
    
}

//-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    UpdateDetailsViewController *updateDetailVC=[[UpdateDetailsViewController alloc] init];
//    [self.navigationController pushViewController:updateDetailVC animated:YES];
//    [updateDetailVC release];
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
